<template>
  <view>
    <!-- 搜索栏子组件 -->
    <my-search-bar />
    <!-- 轮播图 -->
    <swiper indicator-dots autoplay circular :interval="3000">
      <!-- 通过声明式 navigator 跳转到详情页面，并且带上goods_id -->
      <navigator
        v-for="item in swiperList"
        :key="item.goods_id"
        :url="'/subpkg/goods-detail/index?goods_id=' + item.goods_id"
      >
        <swiper-item>
          <image :src="item.image_src" />
        </swiper-item>
      </navigator>
    </swiper>

    <!-- 分类 -->
    <view class="nav-list">
      <image
        v-for="item in categories"
        :key="item.name"
        :src="item.image_src"
        class="nav-img"
        @click="catItemClick(item.name)"
      />
    </view>

    <!-- 楼层 -->
    <view v-for="item in floordata" :key="item.floor_title.name">
      <!-- 头部 -->
      <img
        :src="item.floor_title.image_src"
        class="floor-title"
        mode="scaleToFill"
      />
      <!-- 身体 -->
      <view class="floor-img-box">
        <!-- 左边 -->
        <view>
          <navigator :url="item.product_list[0].navigator_url">
            <image
              :src="item.product_list[0].image_src"
              :style="{ width: item.product_list[0].image_width + 'rpx' }"
              mode="widthFix"
            />
          </navigator>
        </view>
        <!-- 右边 -->
        <view class="right-img-box">
          <navigator
            :url="subitem.navigator_url"
            v-for="(subitem, index) in item.product_list"
            :key="subitem.name"
          >
            <img
              v-show="index > 0"
              :src="subitem.image_src"
              :style="{ width: subitem.image_width + 'rpx' }"
              mode="widthFix"
            />
          </navigator>
        </view>
      </view>
    </view>
  </view>
</template>
<script>
import tabBarBadge from '../../mixins/tabBar-badge'
export default {
  mixins: [tabBarBadge],
  data () {
    return {
      swiperList: [], // 轮播图
      categories: [], // 分类数据
      floordata: [] // 楼层数据
    }
  },
  onLoad (options) {
    this.swiperListApi()
    this.categoriesApi()
    this.floordataApi()
  },
  methods: {
    // 轮播图
    async swiperListApi () {
      const {
        meta: { status },
        message
      } = await uni.$request({
        url: 'home/swiperdata'
      })
      // console.log(res)
      if (status === 200) {
        this.swiperList = message
      }
    },
    // 分类数据
    async categoriesApi () {
      const {
        meta: { status },
        message
      } = await uni.$request({
        url: 'home/catitems'
      })
      // console.log(res)
      if (status === 200) {
        this.categories = message
      }
    },
    // 楼层数据
    async floordataApi () {
      const {
        meta: { status },
        message
      } = await uni.$request({
        url: 'home/floordata'
      })
      // console.log(res)
      if (status === 200) {
        message.forEach(item => {
          item.product_list.forEach(subitem => {
            subitem.navigator_url =
              '/subpkg/goods-detail/index?' +
              subitem.navigator_url.split('?')[1]
          })
        })
        this.floordata = message
      }
    },
    catItemClick (item) {
      if (item !== '分类') return
      uni.switchTab({ url: '/pages/category/index' })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-box {
  // 设置定位效果为“吸顶”
  position: sticky;
  // 吸顶的“位置”
  top: 0;
  // 提高层级，防止被轮播图覆盖
  z-index: 999;
}

swiper {
  height: 400rpx;

  .swiper-item,
  image {
    width: 100%;
    height: 100%;
  }
}

.nav-list {
  display: flex;
  justify-content: space-around;
  margin: 15px 0;

  .nav-img {
    width: 128rpx;
    height: 140rpx;
  }
}

.floor-title {
  height: 60rpx;
  width: 100%;
  display: flex;
}

.right-img-box {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-around;
}

.floor-img-box {
  display: flex;
  padding-left: 10rpx;
}
</style>
